package com.sky.controller.admin;

import com.sky.annotation.AutoFill;
import com.sky.constant.RedisConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.utils.RedisUtil;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * @Author : xiongke
 * @Description:
 * @Date: 2024/8/27
 */
@RestController
@Api(tags = "菜品相关接口")
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {

    @Resource
    DishService dishService;

    @Resource
    private RedisUtil redisUtil;

    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> queryPage(DishPageQueryDTO dto) {
        log.info("菜品分页查询：{}", dto);
        PageResult result = dishService.queryPage(dto);
        return Result.success(result);
    }

    @ApiOperation("菜品起售、停售")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status, Long id) {
        log.info("修改菜品售卖状态：{}", status);
        clearCache(RedisConstant.DISH_CACHE + "*");
        Dish dish = Dish.builder().status(status).id(id).build();
        dishService.updateByConditions(dish);
        return Result.success();
    }

    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<DishVO>> queryByCategoryId(Long categoryId) {
        log.info("根据分类id查询菜品：{}", categoryId);
        List<DishVO> dishVO = dishService.queryByCategoryId(categoryId);
        return Result.success(dishVO);
    }

    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> queryById(@PathVariable Long id) {
        log.info("查询id为：{}的菜品", id);
        DishVO dishVO = dishService.queryById(id);
        return Result.success(dishVO);
    }

    @ApiOperation("新增菜品")
    @PostMapping()
    public Result save(@RequestBody DishDTO dto) {
        log.info("新增菜品：{}", dto);
        clearCache(RedisConstant.DISH_CACHE + "*");
        dishService.saveWithFlavor(dto);
        return Result.success();
    }

    @ApiOperation("批量删除菜品")
    @DeleteMapping()
    public Result deleteBatch(@RequestParam List<Long> ids) {
        log.info("删除菜品：{}", ids);
        clearCache(RedisConstant.DISH_CACHE + "*");
        dishService.deleteBatch(ids);
        return Result.success();
    }

    @ApiOperation("修改菜品")
    @PutMapping
    public Result updateDish(@RequestBody DishDTO dto) {
        log.info("修改菜品：{}", dto);
        clearCache(RedisConstant.DISH_CACHE + "*");
        dishService.updateDishWithFlavor(dto);
        return Result.success();
    }

    private void clearCache(String patten) {
        log.info("清除：{}缓存", patten);
        //删除缓存
        Set keys = redisUtil.getKeys(RedisConstant.DISH_CACHE + "*");
        redisUtil.delAll(keys);
    }

}
